package com.gykj.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.gykj.admin.common.constants.RespResultConstants;
import com.gykj.admin.common.entity.User;
import com.gykj.admin.common.entity.UserRole;
import com.gykj.admin.common.resp.RespResultJson;
import com.gykj.admin.service.UserRoleService;
import com.gykj.admin.service.UserService;
import com.gykj.admin.utils.PasswordHelper;

/**
 * Created by rohto.li on 2017/11/03.
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserRoleService userRoleService;

    @RequestMapping
    public Map<String,Object> getAll(User user, String draw,
                                     @RequestParam(required = false, defaultValue = "1") int start,
                                     @RequestParam(required = false, defaultValue = "10") int length){
        Map<String,Object> map = new HashMap<>();
        PageInfo<User> pageInfo = userService.selectByPage(user, start, length);
        System.out.println("pageInfo.getTotal():"+pageInfo.getTotal());
        map.put("draw",draw);
        map.put("recordsTotal",pageInfo.getTotal());
        map.put("recordsFiltered",pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }
    
    @RequestMapping(value = "/getAllUserList")
    public PageInfo<User> getAllUserList(
    		@RequestBody User user,
            @RequestParam(value="currentPage",required = false, defaultValue = "1") int currentPage,
            @RequestParam(value="pageSize",required = false, defaultValue = "20") int pageSize,
            @RequestParam(value="sortColumn",required = false, defaultValue = "1") int sortColumn,
            @RequestParam(value="sortDesc",required = false, defaultValue = "1") int sortDesc
            )
    {
    	return userService.selectByPage(user, currentPage, pageSize);
    }
    
    @RequestMapping(value = "/getUserById")
    public List<User> getUserById(@RequestParam(value = "uid",required = false) Integer uid) {
        return userService.queryUserListWithSelected(uid);
    }

    /**
     * 保存用户角色
     * @param userRole 用户角色
     * 此处获取的参数的角色id是以 “,” 分隔的字符串
     * @return
     */
    @RequestMapping(value = "/saveUserRoles")
    public RespResultJson saveUserRoles(@RequestBody UserRole userRole) {
    	RespResultJson  result = new RespResultJson();
    	
        if (StringUtils.isEmpty(userRole.getUserId())) {
        	result.setStatus(RespResultConstants.F);
        	result.setMessage(" 参数为空！");
        }
        try {
            userRoleService.addUserRole(userRole);
            result.setStatus(RespResultConstants.T);
        } catch (Exception e) {
            e.printStackTrace();
        	result.setStatus(RespResultConstants.F);
        	result.setMessage(e.getMessage());
        }
         return result;
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public RespResultJson addUser(@RequestBody User user) {
    	RespResultJson  result = new RespResultJson();
        try {
        	//校验是否存在
            if (userService.selectByUsername(user.getUserNameEg()) != null) {
            	result.setStatus(RespResultConstants.F);
            	result.setMessage(user.getUserNameEg()+" 已经存在");
                return result;
            }
           //保存数据
            user.setIsValid(1);
            PasswordHelper passwordHelper = new PasswordHelper();
            passwordHelper.encryptPassword(user);
            userService.save(user);
            
            result.setStatus(RespResultConstants.T);
            result.setMessage("用户： "+user.getUserNameEg()+"添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
        	result.setStatus(RespResultConstants.F);
        	result.setMessage(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
    public RespResultJson deleteUser(@RequestParam(value = "id",required = false) Integer id) {
    	RespResultJson  result = new RespResultJson();
      try {
          userService.deleteUser(id);
          result.setStatus(RespResultConstants.T);
          result.setMessage("删除成功！");
      } catch (Exception e) {
        e.printStackTrace();
      	result.setStatus(RespResultConstants.F);
      	result.setMessage(e.getMessage());
      }
      return result;
    }
}
